package com.lin.fengxiu.util;

import com.lin.fengxiu.model.Order;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Calendar;

/**
 * @ClassName OrderUtil
 * @Description TODO 订单工具类 -- 生成订单号等功能
 * @Author wushaopei
 * @Date 2021/4/6 17:20
 * @Version 1.0
 */
@Component
public class OrderUtil {

    private static String[] yearCodes;

    @Value("${fengxiu.year-codes}")
    public void setYearCodes(String yearCodes){
        String[] chars = yearCodes.split(",");
        OrderUtil.yearCodes = chars;
    }

    /**
     * @Description TODO 订单号生成
     * @return
     */
    public static String makeOrderNo(){

        StringBuffer joiner = new StringBuffer();
        // 生成 Calendar 对象
        Calendar calendar = Calendar.getInstance();
        // 获取时间毫秒值
        String mills = String.valueOf(calendar.getTimeInMillis());
        // 从默认时区中的系统时钟获取当前日期时间
        String micro = LocalDateTime.now().toString();
        // 1000以内三位随机数
        String random = String.valueOf(Math.random()*1000).substring(0,2);
        // 拼接字符生成订单号
        joiner.append(OrderUtil.yearCodes[calendar.get(Calendar.YEAR) - 2019])
                .append(Integer.toHexString(calendar.get(Calendar.MONTH)+1).toUpperCase())
                .append(calendar.get(Calendar.DAY_OF_MONTH))
                .append(mills.substring(mills.length()-5,mills.length()))
                .append(micro.substring(micro.length()-3,micro.length()))
                .append(random);
        return joiner.toString();
    }

}
